package com.example.springboot.utils;

import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;

import static java.util.concurrent.Executors.newScheduledThreadPool;

/**
 * 线程池，默认30个线程
 *
 * @author wangsh
 * @date 2019/8/8 21:16
 * @Description
 */
public class ThreadUtils {

    //线程池大小
    private static final int POOL_SIZE = 30;
    //默认创建30个线程
    private static ScheduledExecutorService executorService = newScheduledThreadPool(POOL_SIZE);

    public static ScheduledExecutorService getThreadPool() {
        return executorService;
    }

    public static void main(String[] args) {

        ScheduledExecutorService threadPool = ThreadUtils.getThreadPool();
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("测试submit线程池： " + POOL_SIZE);
            }
        });

        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("测试execute线程池： " + POOL_SIZE);
            }
        });

        threadPool.submit(new Callable() {
            @Override
            public Object call() throws Exception {
                System.out.println("测试Callable线程池： " + POOL_SIZE);
                return null;
            }
        });

    }
}
